package com.krish.horizontalscrollview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DateTime;
import com.anbinh.doctor.utils.IRcallBak;

public class CustomListAdapter extends ArrayAdapter<DateTime> {
	Context context;
	private ArrayList<DateTime> list;
	int layoutId;
	private LayoutInflater layoutInflater;
	public int currPosition = 0;
	private IRcallBak iRcallBak;

	public CustomListAdapter(Context context, int textViewResourceId,
			ArrayList<DateTime> list, IRcallBak iRcallBak) {
		super(context, android.R.layout.simple_list_item_1, list);
		this.context = context;
		this.list = list;
		this.iRcallBak=iRcallBak;
		layoutId = textViewResourceId;
		layoutInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public DateTime getItem(int position) {
		return list.get(position);
	}
	
	public int getWidth(){
		DisplayMetrics displaymetrics = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay()
				.getMetrics(displaymetrics);
		return  displaymetrics.widthPixels / 8;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutParams params = new LayoutParams(getWidth(),
					LayoutParams.MATCH_PARENT);
			
			convertView = layoutInflater.inflate(layoutId, null, true);
			convertView.setLayoutParams(params);
			
			viewHolder.dayName = (TextView) convertView.findViewById(R.id.dayName);
			viewHolder.date = (TextView) convertView.findViewById(R.id.date);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Log.d("isSelected","isSelected");
		DateTime newsSource = getItem(position);
		if(newsSource.isSelected()){
		
			viewHolder.date.setBackgroundResource(R.drawable.date_active);
			viewHolder.date.setTextColor(Color.parseColor("#ffffff"));
		}else{
			viewHolder.date.setBackgroundResource(android.R.color.transparent);
			viewHolder.date.setTextColor(Color.parseColor("#15c3e8"));
			
		}
		
		viewHolder.dayName.setText(newsSource.getDateName());
		viewHolder.date.setText(newsSource.getDate());
		
		viewHolder.date.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				iRcallBak.onDisplay(position, null);
				
			}
		});

		return convertView;
	}

	private class ViewHolder {
		public TextView dayName;
		public TextView date;
		

	}
	public int getCurrentPosition(){
		return currPosition;
	}
}
